package com.iceframework.hiapp.slice;

import com.iceframework.hiapp.ResourceTable;
import com.iceframework.hiapp.core.utils.HiDBUtils;
import com.iceframework.hiapp.core.utils.HiLogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;

public class DBAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_db);

        // 获取RdbStore对象
        final RdbStore store = HiDBUtils.getRdbStore(DBAbilitySlice.this);
        Text dataText = (Text)findComponentById(ResourceTable.Id_dbText);
        Text sqlText = (Text)findComponentById(ResourceTable.Id_dbExecuteSqlText);
        Button button = (Button)findComponentById(ResourceTable.Id_btnDB);
        if(button != null) {
            button.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    ValuesBucket values = new ValuesBucket();
                    values.putInteger("id", 1);
                    values.putString("name", "Bill");
                    values.putInteger("age", 20);
                    values.putDouble("salary", 12345.1);
                    // 插入记录
                    long id = HiDBUtils.insertOneRowData(store, "test", values);
                    HiLogUtils.error("==================>插入一条数据："+id);
                    String querySQL = "select id,name,age,salary from test where id = 1 order by name asc";
                    ResultSet resultSet = HiDBUtils.query(store, querySQL, null);
                    resultSet.goToNextRow();
                    if(dataText != null) {
                        dataText.setText(resultSet.getString(1)+":"+resultSet.getString(3));
                    }
                    if(dataText != null) {
                        // 跑马灯效果
                        sqlText.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
                        // 始终处于自动滚动状态
                        sqlText.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
                        // 启动跑马灯效果
                        sqlText.startAutoScrolling();
                        sqlText.setText(querySQL);
                    }
                }
            });
        }
    }
}
